<?php

namespace tools\lib\main;

use tools\lib\Parameter;

class Common
{
    /**
     * @var Parameter
     */
    protected $parameter = null;

    /**
     * @var array
     */
    protected $config = [];

    /**
     * 开启自动添加后缀,
     * TestModel,TestLogic
     * @var string
     */
    protected $suffix = true;

    /**
     * 类名
     * @var string
     */
    protected $class_name = '';

    /**
     * @var string
     */
    protected $directory = '';

    /**
     * 设置命名空间
     * @var string
     */
    protected $namespace = '';

    /**
     * 设置继承关系
     * @var string
     */
    protected $extends = '';

    /**
     * Common constructor.
     * @param Parameter $parameter
     */
    public function __construct(Parameter $parameter)
    {
        $this->parameter = $parameter;

        $this->init();
    }

    protected function init()
    {
        $this->setConfig($this->parameter->getType())
            ->setFileDirectory()
            ->setFileNamespace()
            ->setFileExtends();
    }

    /**
     * @param $suffix
     * @param string $file_name
     * @return mixed|string
     */
    protected function getFileProperties($suffix, $file_name = '')
    {
        $file_name = $file_name ? $file_name : lcfirst($this->parameter->getFileName());

        $concat = $file_name . '_' . $suffix;

        $config = '';
        /**
         * 指定的值,如 logic_directory
         */
        if (isset($this->config[$concat])) {
            $config = $this->config[$concat];
        } /**
         * 通配符,如 directory
         */
        elseif (isset($this->config[$suffix])) {
            $config = $this->config[$suffix];
        }

        $name = $this->parameter->getName();
        $separator = strpos($name, '_');
        if ($separator) {
            $config = preg_replace("/($name)/", substr($name, 0, $separator) . '\\\$1', $config);
        }

        return $config;
    }

    /**
     * @param $type
     * @return $this
     */
    protected function setConfig($type)
    {
        $config = config($type);

        /**
         * 替换常量
         */
        if ($config) {
            foreach ($config as &$item) {
                $item = preg_replace(['/__NAME__/', '/__MODULE__/'], [$this->parameter->getName(), $this->parameter->getModule()], $item);
            }
            unset($item);
        }

        $this->config = $config;

        return $this;
    }

    /**
     * @return array
     */
    protected function getConfig()
    {
        return $this->config;
    }

    /**
     * 是否添加类型的后缀,如 xxLogic,xxService
     * @return string
     */
    protected function getSuffix()
    {
        if ($this->suffix) {
            return transToPascal(basename(static::class));
        }
        return '';
    }

    /**
     * @param string $directory
     * @return $this
     */
    protected function setFileDirectory($directory = 'directory')
    {
        $this->directory = $this->getFileProperties($directory);

        return $this;
    }

    /**
     * @return string
     */
    protected function getFileDirectory()
    {
        return $this->directory;
    }

    /**
     * @param string $namespace
     * @return $this
     */
    protected function setFileNamespace($namespace = 'namespace')
    {
        $this->namespace = $this->getFileProperties($namespace);

        return $this;
    }

    /**
     * @return string
     */
    protected function getFileNamespace()
    {
        if ($this->namespace) {
            return 'namespace ' . $this->namespace . ';';
        }
        return '';
    }

    /**
     * @param string $extends
     * @return $this
     */
    protected function setFileExtends($extends = 'extends')
    {
        $this->extends = $this->getFileProperties($extends);

        return $this;
    }

    /**
     * @return string
     */
    protected function getFileExtends()
    {
        if ($this->extends) {
            return 'extends ' . substr($this->extends, strrpos($this->extends, '\\') + 1);
        }

        return '';
    }

    /**
     * @return string
     */
    protected function getFileUse()
    {
        if ($this->extends) {
            return 'use ' . $this->extends . ';';
        }

        return '';
    }

    /**
     * 保存的文件名称
     * @return string
     */
    protected function saveName()
    {
        return $this->className() . '.php';
    }

    /**
     * @return string
     */
    protected function savePath()
    {
        return rtrim($this->getFileDirectory(), '/') . '/' . $this->saveName();
    }

    /**
     * 类名
     * TestModel,TestLogic
     * @return string
     */
    protected function className()
    {
        return transToPascal($this->parameter->getName()) . $this->getSuffix();
    }

    /**
     * 要写入的模板信息
     * @return string
     */
    protected function template()
    {
        return '';
    }

    /**
     * @return mixed
     */
    protected function callback()
    {
        return null;
    }

    /**
     * @return bool
     */
    public function write()
    {
        if (!$template = $this->template()) {
            return false;
        }

        if (!checkAndCreateDirectory($this->getFileDirectory())) {
            dump('目录创建失败:' . $this->getFileDirectory());
            return false;
        }

        $file_path = $this->savePath();

        // 不存在的话,直接创建文件
        if (!is_file($file_path)) {
            if (file_put_contents($file_path, $template)) {
                echo '创建成功' . $file_path . "\n";
            } else {
                echo '创建失败:' . $file_path . "\n";
            };
        }

        /**
         * 创建完成后,执行回调函数(控制器文件存在,可创建方法,目前也主要用于创建方法)
         */
        $this->callback();

        return true;
    }
}